#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : Chestnut
# @time    : 2025/6/27 08:21
# @function: What can I do.
# @version : V1
import subprocess
import tkinter
from tkinter import filedialog


class MyGui:
    def __init__(self):
        pass

    def read_file(self):
        self.t1.delete("1.0", tkinter.END)
        py_files = filedialog.askopenfilenames(filetypes=[("Python Files", "*.py")])
        if len(py_files) == 0:
            self.t1.insert(tkinter.END, "No Files Selected")
            pass
        else:
            for py_file in py_files:
                self.t1.insert(tkinter.END, py_file + "\n")
            self.t1.insert(tkinter.END, f"共选择{len(py_files)}个文件")
            self.file_name.set(py_files[0])
            self.p_files = py_files

    def _execute(self):
        for py_file in self.p_files:
            results = subprocess.run([
                "activate post-data-process",
                f"pyinstaller -F -w {py_file}",
            ],shell=True)
            self.t1.insert(tkinter.END, results.stdout)
            self.t1.insert(tkinter.END, results.stderr)
            self.t1.insert(tkinter.END, results.returncode)

    def main_window(self):
        root = tkinter.Tk()
        self.file_name = tkinter.StringVar()
        root.title("PyInstaller")
        tkinter.Label(root, text="选择文件").grid(row=0, column=0)
        self.e1 = tkinter.Entry(root, width=50, textvariable=self.file_name)
        self.t1 = tkinter.Text(root)
        self.e1.grid(row=0, column=1)
        self.t1.grid(row=1, column=0, columnspan=3)
        tkinter.Button(root, text="Browse", command=self.read_file).grid(row=0, column=2)
        tkinter.Button(root, text="Execute", command=self._execute).grid(row=2, column=0, columnspan=3,
                                                                         sticky=tkinter.EW)

        root.mainloop()


myapp = MyGui()
myapp.main_window()
